#include "facility.h"
#include "user_reservation.h"
//int date[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
//void update(int* year, int* mon, int* mday) {
//    if (*mday > date[*mon]) {
//        *mday = *mday - date[*mon];
//        *mon  = *mon + 1;
//        if (*mon > 12) {
//            *year = *year + 1;
//        }
//    }
//}
// 时间链表初始化
void initTList(TList* t_list) {
    t_list->num = 8;
    t_list->head = (TNode*)malloc(sizeof(TNode));
    TNode* cur = t_list->head;
    // 获取当前时间
    time_t now = 0;
    time(&now);
    struct tm to_tm = *localtime(&now);
    for (int i = 0; i < 8; i++) {
        TNode* temp = (TNode*)malloc(sizeof(TNode));
        temp->next = NULL;
        temp->t.year = to_tm.tm_year + 1900;
        temp->t.month = to_tm.tm_mon + 1;
        temp->t.day = to_tm.tm_mday;
        to_tm.tm_mday += 1;
        mktime(&to_tm);
        //update(&temp->t.year,&temp->t.month,&temp->t.day);
        memset(temp->t.day_time, 0, sizeof(temp->t.day_time));
        cur->next = temp;
        cur = cur->next;
    }
}
// 场地时间链表设置
void set_TList(System* sys) {
    Res_node* res_node = sys->reservation_link.head;
    /*Facility* facility = find_facility(&sys->facility_link, res_node->user_res.num);
    TNode* t_node = facility->time_list.head->next;*/
    while (res_node != NULL) {
        read_res_set_facility(res_node->user_res,sys);
        res_node = res_node->next;
    }
}
// 创建场地
Facility create_facility(System system,int id,char* name,int manager_id,int type,int area,char* open_time,float price,int max_person_num) {
    Facility facility;
    // 时间链表初始化
    initTList(&facility.time_list);
    facility.id = id;
    facility.max_person_num = max_person_num;
    strncpy_s(facility.name,MAX_NAME_LENGTH,name, MAX_NAME_LENGTH-1);
    facility.name[MAX_USERNAME - 1] = '\0';
    //if (manager_id == -1) {
    //    facility.manager = NULL;
    //}
    //else {
    //    // 查找manager
    //    FacilityManager* manager = find_facility_manager(&system.facility_manager_link,manager_id);
    //    facility.manager = manager;
    //}
    facility.manager_id = manager_id;
    facility.type = (VenueType)type;
    facility.area = area;
    strncpy_s(facility.open_time, MAX_NAME_LENGTH,open_time, MAX_NAME_LENGTH-1);
    facility.name[MAX_PASSWORD - 1] = '\0';
    facility.price_per_half_hour = price;
    return facility;
}
// 初始化场地链表
void init_FList(FList* list) {
    list->num = 0;
    list->head = (FNode*)malloc(sizeof(FNode));
    if (list->head == NULL) {
        printf("内存分配失败！\n");
        return;
    }else {
        list->head->next = NULL;
    }
}
// 创建场地节点
FNode* create_FNode(Facility* facility) {
    FNode* new_node = (FNode*)malloc(sizeof(FNode));
    if (new_node == NULL) {
        printf("内存分配失败！\n");
        return NULL;
    }
    new_node->facility = *facility;
    new_node->next = NULL;
    return new_node;
}
// 添加场地
void add_facility(FList* list, Facility* facility) {
    FNode* new_node = create_FNode(facility);
    FNode* temp = list->head;
    while (temp->next != NULL) {
        temp = temp->next;
    }
    temp->next = new_node;
    list->num++;
}
// 删除场地节点
int delete_facility(FList* list, int facility_id) {
    if (list->head->next == NULL) {
        printf("场地链表为空！\n");
        return -1;
    }
    FNode* cur_node = list->head->next;
    FNode* pre_node = NULL;
    // 查找要删除的场地
    while (cur_node != NULL && cur_node->facility.id != facility_id) {
        pre_node = cur_node;
        cur_node = cur_node->next;
    }
    // 如果没有找到该场地
    if (cur_node == NULL) {
        printf("没有找到指定ID的场地！\n");
        return -1;
    }
    // 删除节点
    pre_node->next = cur_node->next;
    free(cur_node);
    list->num--;
    return 0;
}
// 查找场地节点
Facility* find_facility(FList* list, int facility_id) {
    FNode* temp = list->head->next;
    while (temp != NULL) {
        if (temp->facility.id == facility_id) {
            return &temp->facility;
        }
        temp = temp->next;
    }
    return NULL;  // 没有找到场地
}
// 打印链表中的所有场地
void print_facilities(FList* list) {
    if (list->head->next == NULL) {
        printf("场地链表为空！\n");
        return;
    }
    FNode* temp = list->head->next;
    printf("场地列表：\n");
    while (temp != NULL) {
       /* printf("ID: %d, 名称: %s, 类型: %d, 开放时间: %s, 租金: %.2f\n",
            temp->facility.id,
            temp->facility.name,
            temp->facility.type,
            temp->facility.open_time,
            temp->facility.price_per_half_hour);*/
        printf("ID: %d, 名称: %s 场地类型：",
            temp->facility.id,
            temp->facility.name);
        print_facility_type(temp->facility.type);
        printf(" 开放时间: %s, 租金: %.2f,面积：%d\n",
            temp->facility.open_time,
            temp->facility.price_per_half_hour,
            temp->facility.area);
        temp = temp->next;
    }
}

// 清空场地链表
void clear_facility(FList* list) {
    FNode* cur_node = list->head->next;
    FNode* pre_node = list->head;
    while (pre_node->next != NULL) {
        cur_node = pre_node->next;
        pre_node->next = cur_node->next;
        free(cur_node);
    }
}
// 打印场地类型
void print_facility_type(VenueType type) {
    switch (type) {
    case 1:
        printf("篮球场");
        break;
    case 2:
        printf("羽毛球场");
        break;
    case 3:
        printf("足球场");
        break;
    case 4:
        printf("台球场");
        break;
    case 5:
        printf("乒乓球场");
        break;
    case 6:
        printf("排球场");
        break;
    default:
        printf("未知类型");
        break;
    }
}
// 根据类型打印场地
void print_facility_by_type(VenueType type, FList* list) {
    if (list->head->next == NULL) {
        printf("场地链表为空！\n");
        return;
    }
    FNode* temp = list->head->next;
    printf("场地列表：\n");
    while (temp != NULL) {
        if (temp->facility.type == type) {
            printf("ID: %d, 名称: %s 场地类型：",
                temp->facility.id,
                temp->facility.name);
            print_facility_type(temp->facility.type);
            printf(" 开放时间: %s, 租金: %.2f,面积：%d\n",
                temp->facility.open_time,
                temp->facility.price_per_half_hour,
                temp->facility.area);
        }
        temp = temp->next;
    }
}

void print_facility_by_id(int id, FList* list) {
    if (list->head->next == NULL) {
        printf("场地链表为空！\n");
        return;
    }
    FNode* temp = list->head->next;
    printf("场地列表：\n");
    while (temp != NULL)  {
        if (temp->facility.id == id) {
            printf("ID: %d, 名称: %s 场地类型：",
                temp->facility.id,
                temp->facility.name);
            print_facility_type(temp->facility.type);
            printf(" 开放时间: %s, 租金: %.2f,面积：%d\n",
                temp->facility.open_time,
                temp->facility.price_per_half_hour,
                temp->facility.area);
        }
        temp = temp->next;
    }
}
// 打印人流量
bool print_time_list(Facility facility, int year, int month, int day) {
    TNode* t_node = facility.time_list.head->next;
    t_node = t_node->next;
    int index = 0;      //判断有无
    while (t_node!=NULL) {
        if (t_node->t.day == day &&t_node->t.month == month && t_node->t.year == year) {
            index = 1;
            printf("营业时间：%s\t最大容纳人数：%d人\n", facility.open_time,facility.max_person_num);
            printf("08:00   09:00   10:00   11:00   12:00   13:00   14:00\n");
            for (int j = 0; j < 14; j++) {
                //printf("  %d ", facility.time_list.head->next->t.day_time[j]);
                printf("  %d ", t_node->t.day_time[j]);
            }
            printf( "\n15:00   16:00   17:00   18:00   19:00   20:00   21:00\n");
            for (int j = 14; j < 28; j++) {
                //printf("  %d ", facility.time_list.head->next->t.day_time[j]);
                printf("  %d ", t_node->t.day_time[j]);
            }
            printf("\n");
            break;
        }
        else {
            t_node = t_node->next;
        }
    }
    if (index == 0) {
        printf("预约日期不合法\n");
        system("pause");
        return false;
    }
    return true;
}
// 判断 某个场地某个时间是否有空
bool judge_facility_free(Facility* facility, int year, int month, int day, char* start_time, char* end_time) {
    char start_t[6];
    char end_t[6];
    strncpy_s(start_t, 6, start_time, 5);
    strncpy_s(end_t, 6, end_time, 5);
    start_t[5] = '\0';
    end_t[5] = '\0';
    int start = index_time(start_t);
    int end = index_time(end_t);
    TNode* t_node = facility->time_list.head->next;
    while (t_node != NULL) {
        if (t_node->t.year == year && t_node->t.month == month && t_node->t.day == day) {
            for (int i = start; i < end; i++) {
                if (t_node->t.day_time[i] >= facility->max_person_num) {
                    return false;
                }
            }
            return true;
        }
        t_node = t_node->next;
    }
    return false;
}
// 场地某个时间段 人数+1
void add_facility_people(Facility* facility, int year, int month, int day, char* start_time, char* end_time) {
    char start_t[6];
    char end_t[6];
    strncpy_s(start_t, 6, start_time, 5);
    strncpy_s(end_t, 6, end_time, 5);
    start_t[5] = '\0';
    end_t[5] = '\0';
    int start = index_time(start_t);
    int end = index_time(end_t);
    TNode* t_node = facility->time_list.head->next;
    while (t_node != NULL) {
        if (t_node->t.year == year && t_node->t.month == month && t_node->t.day == day) {
            for (int i = start; i < end; i++) {
                t_node->t.day_time[i] += 1;
            }
        }
        t_node = t_node->next;
    }
}

//排序功能
// 查看场地 Order by 编号
void sort_facility_order_by_id(FList* list) {
    // 咩有和只有一个都不需要排序
    if (!list->head || !list->head->next ||!list->head->next->next) return;
    else {
        bool swapped = true;
        FNode* ptr;
        while (swapped) {
            swapped = false;
            ptr = list->head->next;
            while (ptr->next != NULL) {
                Facility cur = ptr->facility;
                Facility next = ptr->next->facility;
                if (cur.id - next.id > 0) {
                    ptr->facility = next;
                    ptr->next->facility = cur;
                    swapped = true;
                }
                ptr = ptr->next;
            }
        }
    }
}
// 查看场地 Order by 面积
void sort_facility_order_by_area(FList* list) {
    // 咩有和只有一个都不需要排序
    if (!list->head || !list->head->next || !list->head->next->next) return;
    else {
        bool swapped = true;
        FNode* ptr;
        while (swapped) {
            swapped = false;
            ptr = list->head->next;
            while (ptr->next != NULL) {
                Facility cur = ptr->facility;
                Facility next = ptr->next->facility;
                if (cur.area - next.area > 0) {
                    ptr->facility = next;
                    ptr->next->facility = cur;
                    swapped = true;
                }
                ptr = ptr->next;
            }
        }
    }
}
// 查看场地 Order by 费用
void sort_facility_order_by_fee(FList* list) {
    // 咩有和只有一个都不需要排序
    if (!list->head || !list->head->next || !list->head->next->next) return;
    else {
        bool swapped = true;
        FNode* ptr;
        while (swapped) {
            swapped = false;
            ptr = list->head->next;
            while (ptr->next != NULL) {
                Facility cur = ptr->facility;
                Facility next = ptr->next->facility;
                if (cur.price_per_half_hour - next.price_per_half_hour > 0) {
                    ptr->facility = next;
                    ptr->next->facility = cur;
                    swapped = true;
                }
                ptr = ptr->next;
            }
        }
    }
}
// 查看场地 Order by 类型
void sort_facility_order_by_type(FList* list) {
    // 咩有和只有一个都不需要排序
    if (!list->head || !list->head->next || !list->head->next->next) return;
    else {
        bool swapped = true;
        FNode* ptr;
        while (swapped) {
            swapped = false;
            ptr = list->head->next;
            while (ptr->next != NULL) {
                Facility cur = ptr->facility;
                Facility next = ptr->next->facility;
                if (cur.type - next.type > 0) {
                    ptr->facility = next;
                    ptr->next->facility = cur;
                    swapped = true;
                }
                ptr = ptr->next;
            }
        }
    }
}



void query_facility_by_type(System sys) {
    printf("查询结果（按类型）：");

    int type; // 场地类型
    printf("请输入场地类型（如篮球场，留空表示不限制）：");
    scanf_s("%d", &type);
    clear_input_buffer();

    FList* list = &sys.facility_link; // 假设场地链表存储在系统结构体中
    query_facility_with_type(list, type);
}

void query_facility_with_type(FList* list, VenueType type) {
    if (list->head == NULL || list->head->next == NULL) {
        printf("没有找到符合条件的场地\n");
        return;
    }

    int found = 0;
    printf("-----------------------------------------------------------------------------------\n");
    printf("%-10s %-20s %-10s %-10s %-10s\n ", "ID", "名称", "面积(㎡)", "价格(元 / 半小时)", "类型");
    printf("-----------------------------------------------------------------------------------\n");

    FNode* node = list->head->next;
    while (node != NULL) {
        Facility* f = &node->facility;
        if (!type || f->type==type) {
            printf("%-10d %-20s %-10d %-10.2f ",
                f->id, f->name,
                
                f->area, f->price_per_half_hour
                );
            print_facility_type(f->type);
            printf("\n");
            found = 1;
        }
        node = node->next;
    }

    if (!found) {
        printf("没有找到符合条件的场地！\n");
    }
    printf("-----------------------------------------------------------------------------------\n");
}


// 场地查找

void query_facility_by_max_person(System sys) {
    printf("查询结果（按最大容纳人数）：");

    int max_person = 0;
    printf("请输入最大容纳人数（输入0表示不限制）：");
    scanf_s("%d", &max_person);
    clear_input_buffer();

    FList* list = &sys.facility_link; // 假设场地链表存储在系统结构体中
    query_facility_with_max_person(list, max_person);
}

void query_facility_with_max_person(FList* list, int max_person) {
    if (list->head == NULL || list->head->next == NULL) {
        printf("没有找到符合条件的场地\n");
        return;
    }

    int found = 0;
    printf("-----------------------------------------------------------------------------------\n");
    printf("%-10s %-20s %-10s %-10s %-10s\n ", "ID", "名称", "面积(㎡)", "价格(元 / 半小时)", "类型");
    printf("-----------------------------------------------------------------------------------\n");

    FNode* node = list->head->next;
    while (node != NULL) {
        Facility* f = &node->facility;
        if (!max_person || f->max_person_num == max_person) {
            printf("%-10d %-20s %-10d %-10.2f",
                f->id, f->name,
                f->area, f->price_per_half_hour);
            print_facility_type(f->type);
            printf("\n");
            found = 1;
        }
        node = node->next;
    }

    if (!found) {
        printf("没有找到符合条件的场地！\n");
    }
    printf("-----------------------------------------------------------------------------------\n");
}

void query_facility_by_id(System sys) {
    printf("查询结果（按场地ID）：");

    int id = 0;
    printf("请输入场地ID：");
    scanf_s("%d", &id);
    clear_input_buffer();

    FList* list = &sys.facility_link; // 假设场地链表存储在系统结构体中
    FNode* node = list->head->next;
    int found = 0;

    printf("-----------------------------------------------------------------------------------\n");
    printf("%-10s %-20s %-10s %-10s %-10s\n ", "ID", "名称", "面积(㎡)", "价格(元 / 半小时)", "类型");
    printf("-----------------------------------------------------------------------------------\n");

    while (node != NULL) {
        Facility* f = &node->facility;
        if (f->id == id) {
            printf("%-10d %-20s %-10d %-10.2f",
                f->id, f->name,
                
                f->area, f->price_per_half_hour
                );
            print_facility_type(f->type);
            printf("\n");
            found = 1;
            break;
        }
        node = node->next;
    }

    if (!found) {
        printf("未找到ID为 %d 的场地！\n", id);
    }
    printf("-----------------------------------------------------------------------------------\n");
}

void fuzzy_search_facility(System sys) {
    printf("模糊查询结果（按场地名称关键字）：");

    char keyword[50] = { 0 };
    printf("请输入场地名称关键字：");
    scanf_s("%49s", keyword, 50);
    clear_input_buffer();

    FList* list = &sys.facility_link; // 假设场地链表存储在系统结构体中
    query_facility_with_keyword(list, keyword);
}

void query_facility_with_keyword(FList* list, const char* keyword) {
    if (list->head == NULL || list->head->next == NULL) {
        printf("没有找到符合条件的场地\n");
        return;
    }

    int found = 0;
    printf("-----------------------------------------------------------------------------------\n");
    printf("%-10s %-20s %-10s %-10s %-10s\n ", "ID", "名称", "面积(㎡)", "价格(元 / 半小时)", "类型");
    printf("-----------------------------------------------------------------------------------\n");

    FNode* node = list->head->next;
    while (node != NULL) {
        Facility* f = &node->facility;
        if (!keyword || strstr(f->name, keyword)) {
            printf("%-10d %-20s %-10d %-10.2f",
                f->id, f->name,
                
                f->area, f->price_per_half_hour
                );
            print_facility_type(f->type);
            printf("\n");
            found = 1;
        }
        node = node->next;
    }

    if (!found) {
        printf("没有找到符合条件的场地！\n");
    }
    printf("-----------------------------------------------------------------------------------\n");
}

void query_facility_by_area(System sys) {
    printf("查询结果（按面积）：");

    int area = 0;
    printf("请输入场地面积（输入0表示不限制）：");
    scanf_s("%d", &area);
    clear_input_buffer();

    FList* list = &sys.facility_link; // 假设场地链表存储在系统结构体中
    query_facility_with_area(list, area);
}

void query_facility_with_area(FList* list, int area) {
    if (list->head == NULL || list->head->next == NULL) {
        printf("没有找到符合条件的场地\n");
        return;
    }

    int found = 0;
    printf("-----------------------------------------------------------------------------------\n");
    printf("%-10s %-20s %-10s %-10s %-10s\n ", "ID", "名称", "面积(㎡)", "价格(元 / 半小时)", "类型");
    printf("-----------------------------------------------------------------------------------\n");

    FNode* node = list->head->next;
    while (node != NULL) {
        Facility* f = &node->facility;
        if (!area || f->area == area) {
            printf("%-10d %-20s %-10d %-10.2f",
                f->id, f->name,
                
                f->area, f->price_per_half_hour
                );
            print_facility_type(f->type);
            printf("\n");
            found = 1;
        }
        node = node->next;
    }

    if (!found) {
        printf("没有找到符合条件的场地！\n");
    }
    printf("-----------------------------------------------------------------------------------\n");
}

void query_facility_by_price(System sys) {
    printf("查询结果（按价格范围）：");

    float min_price = 0.0, max_price = 0.0;
    printf("请输入最低价格（输入0表示不限制）：");
    scanf_s("%f", &min_price);
    printf("请输入最高价格（输入0表示不限制）：");
    scanf_s("%f", &max_price);
    clear_input_buffer();

    FList* list = &sys.facility_link; // 假设场地链表存储在系统结构体中
    query_facility_with_price(list, min_price, max_price);
}

void query_facility_with_price(FList* list, float min_price, float max_price) {
    if (list->head == NULL || list->head->next == NULL) {
        printf("没有找到符合条件的场地\n");
        return;
    }

    int found = 0;
    printf("-----------------------------------------------------------------------------------\n");
    printf("%-10s %-20s %-10s %-10s %-10s\n ", "ID", "名称", "面积(㎡)", "价格(元 / 半小时)", "类型");
    printf("-----------------------------------------------------------------------------------\n");

    FNode* node = list->head->next;
    while (node != NULL) {
        Facility* f = &node->facility;
        if ((!min_price || f->price_per_half_hour >= min_price) &&
            (!max_price || f->price_per_half_hour <= max_price)) {
            printf("%-10d %-20s %-10d %-10.2f",
                f->id, f->name,
                
                f->area, f->price_per_half_hour
                );
            print_facility_type(f->type);
            printf("\n");
            found = 1;
        }
        node = node->next;
    }

    if (!found) {
        printf("没有找到符合条件的场地！\n");
    }
    printf("-----------------------------------------------------------------------------------\n");
}

void query_facility_by_type_price(System sys) {
    printf("查询结果（按类型和价格）：");

    int type; // 场地类型
    printf("请输入场地类型（如篮球场，留空表示不限制）：");
    scanf_s("%d",&type);

    clear_input_buffer();

    float min_price = 0.0, max_price = 0.0;
    printf("请输入最低价格（输入0表示不限制）：");
    scanf_s("%f", &min_price);
    printf("请输入最高价格（输入0表示不限制）：");
    scanf_s("%f", &max_price);
    clear_input_buffer();

    FList* list = &sys.facility_link; // 假设场地链表存储在系统结构体中
    query_facility_with_type_price(list, type,min_price, max_price);
}

void query_facility_with_type_price(FList* list, VenueType type, float min_price, float max_price) {
    if (list->head == NULL || list->head->next == NULL) {
        printf("没有找到符合条件的场地\n");
        return;
    }

    int found = 0;
    printf("-----------------------------------------------------------------------------------\n");
    printf("%-10s %-20s %-10s %-10s %-10s\n ", "ID", "名称", "面积(㎡)", "价格(元 / 半小时)", "类型");
    printf("-----------------------------------------------------------------------------------\n");

    FNode* node = list->head->next;
    while (node != NULL) {
        Facility* f = &node->facility;
        if (((!min_price || f->price_per_half_hour >= min_price) &&
            (!max_price || f->price_per_half_hour <= max_price)) && (!type || f->type==type)){
            printf("%-10d %-20s %-10d %-10.2f",
                f->id, f->name,

                f->area, f->price_per_half_hour
            );
            print_facility_type(f->type);
            printf("\n");
            found = 1;
        }
        node = node->next;
    }

    if (!found) {
        printf("没有找到符合条件的场地！\n");
    }
    printf("-----------------------------------------------------------------------------------\n");
}

void query_facility_by_type_area(System sys) {
    printf("查询结果（按类型和面积）：");
    int type; // 场地类型
    printf("请输入场地类型（如篮球场，留空表示不限制）：");
    scanf_s("%d",&type);
    clear_input_buffer();

    int area = 0;
    printf("请输入场地面积（输入0表示不限制）：");
    scanf_s("%d", &area);
    clear_input_buffer();

    FList* list = &sys.facility_link; // 假设场地链表存储在系统结构体中
    query_facility_with_type_area(list,type, area);
}

void query_facility_with_type_area(FList* list, VenueType type, int area) {
    if (list->head == NULL || list->head->next == NULL) {
        printf("没有找到符合条件的场地\n");
        return;
    }

    int found = 0;
    printf("-----------------------------------------------------------------------------------\n");
    printf("%-10s %-20s %-10s %-10s %-10s\n ", "ID", "名称", "面积(㎡)", "价格(元 / 半小时)", "类型");
    printf("-----------------------------------------------------------------------------------\n");

    FNode* node = list->head->next;
    while (node != NULL) {
        Facility* f = &node->facility;
        if ((!area || f->area == area)&& (!type || f->type==type)) {
            printf("%-10d %-20s %-10d %-10.2f",
                f->id, f->name,

                f->area, f->price_per_half_hour
            );
            print_facility_type(f->type);
            printf("\n");
            found = 1;
        }
        node = node->next;
    }

    if (!found) {
        printf("没有找到符合条件的场地！\n");
    }
    printf("-----------------------------------------------------------------------------------\n");
}

// 打印场地信息
void print_facility(FNode* f_node) {
    if(f_node != NULL) {
        printf("ID: %d, 名称: %s 场地类型：",
            f_node->facility.id,
            f_node->facility.name);
        print_facility_type(f_node->facility.type);
        printf(" 开放时间: %s, 租金: %.2f,面积：%d\n",
            f_node->facility.open_time,
            f_node->facility.price_per_half_hour,
            f_node->facility.area);
    }
}

// 统计功能
// 统计场地 根据类型
void statistic_facility_by_type(System sys) {
    int type;
    int cnt = 0;
    int sum_area = 0;
    printf(" 篮球场-1 羽毛球场-2 足球场-3 台球场-4 乒乓球场-5 排球场-6\n");
    printf(" 请输入类型：");
    scanf_s("%d", &type);
    clear_input_buffer();
    FNode* cur_node = sys.facility_link.head->next;
    while (cur_node != NULL) {
        if (cur_node->facility.type == type) {
            cnt++;
            sum_area += cur_node->facility.area;
            print_facility(cur_node);
        }
        cur_node = cur_node->next;
    }
    printf(" 总计%d个场地 总计面积%d平方米\n", cnt,sum_area);
}
// 统计场地 根据价格
void statistic_facility_by_price(System sys) {
    float price;
    int sum_area = 0;
    int cnt = 0;
    printf(" 请输入价格：");
    scanf_s("%f", &price);
    clear_input_buffer();
    FNode* cur_node = sys.facility_link.head->next;
    while (cur_node != NULL) {
        if (cur_node->facility.price_per_half_hour == price) {
            cnt++;
            sum_area += cur_node->facility.area;
            print_facility(cur_node);
        }
        cur_node = cur_node->next;
    }
    printf(" 总计%d个场地 总计面积%d平方米\n", cnt, sum_area);
}
// 统计场地 根据面积
void statistic_facility_by_area(System sys) {
    int area;
    int sum_area = 0;
    int cnt = 0;
    printf(" 请输入面积：");
    scanf_s("%d", &area);
    clear_input_buffer();
    FNode* cur_node = sys.facility_link.head->next;
    while (cur_node != NULL) {
        if (cur_node->facility.area == area) {
            cnt++;
            sum_area += cur_node->facility.area;
            print_facility(cur_node);
        }
        cur_node = cur_node->next;
    }
    printf(" 总计%d个场地 总计面积%d平方米\n", cnt, sum_area);
}
// 统计场地 根据类型 + 价格
void statistic_facility_by_type_price(System sys) {
    float price;
    int sum_area = 0;
    int cnt = 0;
    printf(" 请输入价格：");
    scanf_s("%f", &price);
    clear_input_buffer();
    int type;
    printf(" 篮球场-1 羽毛球场-2 足球场-3 台球场-4 乒乓球场-5 排球场-6\n");
    printf(" 请输入类型：");
    scanf_s("%d", &type);
    FNode* cur_node = sys.facility_link.head->next;
    while (cur_node != NULL) {
        if (cur_node->facility.price_per_half_hour == price&&
            cur_node->facility.type == type) {
            cnt++;
            sum_area += cur_node->facility.area;
            print_facility(cur_node);
        }
        cur_node = cur_node->next;
    }
    printf(" 总计%d个场地 总计面积%d平方米\n", cnt, sum_area);
}
// 统计场地 根据类型 + 面积
void statistic_facility_by_type_area(System sys) {
    int area;
    int cnt = 0;
    int sum_area = 0;
    printf(" 请输入面积：");
    scanf_s("%d", &area);
    clear_input_buffer();
    int type;
    printf(" 篮球场-1 羽毛球场-2 足球场-3 台球场-4 乒乓球场-5 排球场-6\n");
    printf(" 请输入类型：");
    scanf_s("%d", &type);
    FNode* cur_node = sys.facility_link.head->next;
    while (cur_node != NULL) {
        if (cur_node->facility.area == area &&
            cur_node->facility.type == type) {
            cnt++;
            sum_area += cur_node->facility.area;
            print_facility(cur_node);
        }
        cur_node = cur_node->next;
    }
    printf(" 总计%d个场地 总计面积%d平方米\n", cnt, sum_area);
}
// 统计场地 根据面积范围
void statistic_facility_by_area_range(System sys) {
    int area_min,area_max;
    int sum_area = 0;
    int cnt = 0;
    printf(" 请输入最小面积：");
    scanf_s("%d", &area_min);
    clear_input_buffer();
    printf(" 请输入最大面积：");
    scanf_s("%d", &area_max);
    clear_input_buffer();
    FNode* cur_node = sys.facility_link.head->next;
    while (cur_node != NULL) {
        if (cur_node->facility.area <= area_max&&
            cur_node->facility.area >= area_min) {
            cnt++;
            sum_area += cur_node->facility.area;
            print_facility(cur_node);
        }
        cur_node = cur_node->next;
    }
    printf(" 总计%d个场地 总计面积%d平方米\n", cnt, sum_area);
}
// 统计场地 根据价格范围
void statistic_facility_by_price_range(System sys) {
    float price_min, price_max;
    int sum_area = 0;
    int cnt = 0;
    printf(" 请输入最低价格：");
    scanf_s("%f", &price_min);
    clear_input_buffer();
    printf(" 请输入最高价格：");
    scanf_s("%f", &price_max);
    clear_input_buffer();
    FNode* cur_node = sys.facility_link.head->next;
    while (cur_node != NULL) {
        if (cur_node->facility.price_per_half_hour <= price_max &&
            cur_node->facility.price_per_half_hour >= price_min) {
            cnt++;
            sum_area += cur_node->facility.area;
            print_facility(cur_node);
        }
        cur_node = cur_node->next;
    }
    printf(" 总计%d个场地 总计面积%d平方米\n", cnt, sum_area);
}